package com.java.second_goods;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 启动程序
 *
 * @author clothing-rental
 */
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.java.second_goods.mapper")
public class Main {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  校园二手交易启动成功   ლ(´ڡ`ლ)ﾞ");

        Environment env = context.getEnvironment();
        String port = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "/");
        String swaggerPath = env.getProperty("springdoc.swagger-ui.path", "/swagger-ui.html");

        // 如果contextPath以/结尾且swaggerPath也以/开头，则去掉一个/
        if (contextPath.endsWith("/") && swaggerPath.startsWith("/")) {
            swaggerPath = swaggerPath.substring(1);
        }

        // 如果contextPath不以/结尾且swaggerPath不以/开头，则添加/
        if (!contextPath.endsWith("/") && !swaggerPath.startsWith("/")) {
            swaggerPath = "/" + swaggerPath;
        }

        String swaggerUrl = "http://localhost:" + port + contextPath + swaggerPath;

        System.out.println("系统接口文档: " + swaggerUrl);
        System.out.println("系统访问地址: http://localhost:" + port + contextPath);

        try {

            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + swaggerUrl);
            System.out.println("已自动打开Swagger文档，如未打开请手动访问上面的URL");
        } catch (Exception e) {

            System.out.println("无法自动打开浏览器，请手动访问上面的URL");
        }
    }
}